# 基于 Hyperf 官方镜像
FROM hyperf/hyperf:8.1-alpine-v3.18-swoole

# 设置环境变量
ENV TZ=Asia/Shanghai

# 设置 apk 镜像源（使用阿里云镜像加速）
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories

# 安装基础工具和依赖
RUN apk update && apk add --no-cache \
    openssh \
    supervisor \
    tzdata \
    curl \
    vim \
    bash \
    gcc \
    g++ \
    make \
    linux-headers \
    pcre-dev \
    pcre2-dev \
    zlib-dev \
    openssl-dev \
    wget \
    tar \
    autoconf \
    automake \
    libtool \
    $PHPIZE_DEPS

# 修改时区
RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \
    echo "Asia/Shanghai" > /etc/timezone

# 配置 SSH 服务
RUN mkdir -p /root/.ssh && \
    chmod 700 /root/.ssh && \
    sed -i "s/#PermitRootLogin.*/PermitRootLogin yes/g" /etc/ssh/sshd_config && \
    sed -i "s/#PubkeyAuthentication.*/PubkeyAuthentication yes/g" /etc/ssh/sshd_config && \
    sed -i "s/PasswordAuthentication.*/PasswordAuthentication no/g" /etc/ssh/sshd_config && \
    ssh-keygen -t dsa -P "" -f /etc/ssh/ssh_host_dsa_key -N "" || true && \
    ssh-keygen -t rsa -P "" -f /etc/ssh/ssh_host_rsa_key -N "" || true && \
    ssh-keygen -t ecdsa -P "" -f /etc/ssh/ssh_host_ecdsa_key -N "" || true && \
    ssh-keygen -t ed25519 -P "" -f /etc/ssh/ssh_host_ed25519_key -N "" || true

# 复制 authorized_keys
COPY authorized_keys /root/.ssh/authorized_keys
RUN chmod 600 /root/.ssh/authorized_keys

# 安装 Redis
RUN mkdir -p /opt/data/redis && \
    cd /opt/data/redis && \
    wget -q -O redis-7.2.3.tar.gz http://download.redis.io/releases/redis-7.2.3.tar.gz && \
    tar xzf redis-7.2.3.tar.gz && \
    cd redis-7.2.3 && \
    make 1>/dev/null && \
    make PREFIX=/opt/data/redis install && \
    rm -rf /opt/data/redis/redis-* && \
    mkdir -p /opt/data/redis/conf && \
    mkdir -p /opt/data/redis/log && \
    mkdir -p /opt/data/redis/run && \
    ln -s /opt/data/redis/bin/redis-cli /usr/bin/redis-cli && \
    ln -s /opt/data/redis/bin/redis-server /usr/bin/redis-server

# 复制 Redis 配置文件
COPY redis/conf/redis.conf /opt/data/redis/conf/redis.conf

# 安装 PHP Redis 扩展
# 检查是否已加载，如果未加载则安装并配置
RUN if php -m | grep -q redis; then \
        echo "Redis extension already loaded, skipping installation"; \
    else \
        (pecl install redis || true) && \
        if [ -f /usr/lib/php81/modules/redis.so ]; then \
            echo "extension=redis.so" > /etc/php81/conf.d/redis.ini; \
        elif [ -f /usr/local/lib/php/extensions/*/redis.so ]; then \
            docker-php-ext-enable redis 2>/dev/null || \
            (PHP_INI_DIR=$(php-config --ini-dir 2>/dev/null || echo "/usr/local/etc/php/conf.d") && \
             echo "extension=redis.so" > ${PHP_INI_DIR}/redis.ini); \
        fi; \
    fi

# 安装 Nginx
RUN mkdir -p /opt/data/nginx && \
    cd /opt/data/nginx && \
    wget -q -O nginx-1.25.4.tar.gz http://nginx.org/download/nginx-1.25.4.tar.gz && \
    tar zxvf nginx-1.25.4.tar.gz && \
    cd nginx-1.25.4 && \
    ./configure --prefix=/opt/data/nginx \
        --user=root \
        --group=root \
        --with-http_v2_module \
        --with-http_ssl_module \
        --with-http_sub_module \
        --with-http_stub_status_module \
        --with-http_gzip_static_module \
        --with-pcre \
        --with-http_realip_module && \
    make && \
    make install && \
    rm -rf /opt/data/nginx/nginx-* && \
    mkdir -p /opt/data/nginx/conf/conf.d && \
    mkdir -p /opt/data/nginx/log && \
    mkdir -p /opt/data/nginx/run && \
    ln -s /opt/data/nginx/sbin/nginx /usr/bin/nginx

# 复制 Nginx 配置文件
COPY nginx/nginx.conf /opt/data/nginx/conf/nginx.conf
COPY nginx/conf.d /opt/data/nginx/conf/conf.d

# 配置 Supervisor
RUN mkdir -p /etc/supervisor.d && \
    mkdir -p /var/log/supervisor && \
    mkdir -p /var/run/supervisor

# 复制 Supervisor 配置文件
COPY supervisor/supervisord.conf /etc/supervisord.conf
COPY supervisor/conf.d /etc/supervisor.d

# 设置目录权限
RUN chmod -R 755 /opt/data/redis/log && \
    chmod -R 755 /opt/data/redis/run && \
    chmod -R 755 /opt/data/nginx/log 2>/dev/null || true

# 创建启动脚本
RUN printf '#!/bin/sh\n\n# 启动 Supervisor（前台运行）\necho "Starting supervisor..."\nexec supervisord -c /etc/supervisord.conf -n\n' > /init.sh && \
    chmod +x /init.sh

# 设置工作目录
WORKDIR /www/wwwroot

# 暴露端口
EXPOSE 22 9501 6379 80

# 启动脚本
ENTRYPOINT ["/init.sh"]

